package com.qx.chat.system.event;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.mongodb.core.query.Update;

import com.alibaba.fastjson.JSONArray;
import com.qx.chat.akka.Qxchat;
import com.qx.chat.system.dao.AbstractRepository;
import com.qx.chat.system.dao.UserDaoImpl;
import com.qx.chat.system.entity.User;
import com.qx.chat.system.service.ContactsService;
import com.qx.chat.system.service.ContactsServiceImpl;
import com.qx.chat.system.service.OrgService;
import com.qx.chat.system.service.OrgServiceImpl;
import com.qx.chat.system.util.SpringUtils;
import com.qx.chat.websocket.interceptor.QxWebSocketHandshakeInterceptor;

import akka.actor.ActorRef;


/**
 * 用户自主行为相关事件
 * 
 * @author cyf
 * 
 */
public class UserEvent {

	/**
	 * 连接打开通知登录
	 * 
	 * @author cyf
	 * 
	 */
	public static class SessionOpen extends Event implements Serializable {
		private static final long serialVersionUID = -8128844390551755340L;
		public final static String TYPE = "SessionOpen";
		private boolean open;
		public SessionOpen() {
			super();
			super.setType(TYPE);
		}
		public boolean isOpen() {
			return open;
		}
		public void setOpen(boolean open) {
			this.open = open;
		}
		
	}
	/**
	 * 用户登录事件
	 * 
	 * @author cyf
	 * 
	 */
	public static class Login extends Event implements Serializable {

		private static final long serialVersionUID = -7430185558396236914L;
		private static final Logger LOGGER = LoggerFactory.getLogger(QxWebSocketHandshakeInterceptor.class);
		public final static String TYPE = "Login";
		private User user;
		private String loginName;
		private String password;
		private boolean status = false;
		private String errorMsg;

		public Login() {
			super();
			super.setType(TYPE);
		}
		public Login(User user) {
			super();
			super.setType(TYPE);
			this.user = user;
		}
		public Login(String loginName,String password) {
			super();
			super.setType(TYPE);
			this.loginName = loginName;
			this.password = password;
		}
		public boolean authentication(){
			AbstractRepository<User> userDao = SpringUtils.getBean(UserDaoImpl.class);
			User user = (User) userDao.findOne("loginName", loginName);
			if(user!=null){
				if(user.getPassword()==password||user.getPassword().equals(password)){
					this.status = true;
					this.user = user;
					LOGGER.info(loginName+" 登录系统");
					//更新数据库用户状态
					userDao.findAndModify(user.getId(), new Update().set("online", 1));
					return true;
				}else{
					this.errorMsg = "密码错误";
					// 获取对方actor
			        ActorRef userActor = Qxchat.getInstance().getActorByLoginName(loginName);
			        // 销毁actor
					Qxchat.getInstance().getAkkaSystem().stop(userActor);
					return false;
				}
			}else{
				this.errorMsg = "用户不存在";
				// 获取对方actor
		        ActorRef userActor = Qxchat.getInstance().getActorByLoginName(loginName);
		        // 销毁actor
				Qxchat.getInstance().getAkkaSystem().stop(userActor);
				return false;
			}
		}
		public User getUser() {
			return user;
		}
		public void setUser(User user) {
			this.user = user;
		}
		public boolean isStatus() {
			return status;
		}
		public void setStatus(boolean status) {
			this.status = status;
		}
		public String getErrorMsg() {
			return errorMsg;
		}
		public void setErrorMsg(String errorMsg) {
			this.errorMsg = errorMsg;
		}
	}

	/**
	 * 好友数据
	 * 
	 * @author cyf
	 * 
	 */
	public static class Firends extends Event implements Serializable {

		public final static String TYPE = "Firends";
		private static final long serialVersionUID = 6082670347668980110L;
		private Map<String, JSONArray> firends = new HashMap<String, JSONArray>();
		private String loginName;
		
		public Firends(String loginName) {
			super();
			super.setType("Firends");
			this.loginName = loginName;
		}
		public void findAll(){
			OrgService orgService = SpringUtils.getBean(OrgServiceImpl.class);
			JSONArray findOrgTreeJsonData = (JSONArray)  orgService.findOrgTreeJsonData();
			this.firends.put("orgTreeData",findOrgTreeJsonData);
			ContactsService contactsService = SpringUtils.getBean(ContactsServiceImpl.class);
			JSONArray recentContactsData = contactsService.getRecentContactsTreeDataByLoginName(loginName);
			this.firends.put("recentContactsData",recentContactsData);
			JSONArray personalBandTreeData = contactsService.getPersonalBandTreeDataByLoginName(loginName);
			this.firends.put("personalBandTreeData",personalBandTreeData);
			JSONArray groupTreeData = contactsService.getGroupTreeDataByLoginName(loginName);
			this.firends.put("groupTreeData",groupTreeData);
		}
		public Map<String, JSONArray> getFirends() {
			return firends;
		}

		public void setFirends(Map<String, JSONArray> firends) {
			this.firends = firends;
		}

	}

	/**
	 * 用户状态事件
	 * 
	 * @author cyf
	 * 
	 */
	public static class Status extends Event implements Serializable {

		private static final long serialVersionUID = -4855552674859219156L;
		private int status;
		private String userId;
		private String loginName;
		public final static String TYPE = "Status";

		public Status(String userId,String loginName,int status) {
			super();
			super.setType("Status");
			this.userId = userId;
			this.status = status;;
			this.loginName = loginName;
		}

		public String getUserId() {
			return userId;
		}

		public void setUserId(String userId) {
			this.userId = userId;
		}

		public String getLoginName() {
			return loginName;
		}

		public void setLoginName(String loginName) {
			this.loginName = loginName;
		}

		public int getStatus() {
			return status;
		}

		public void setStatus(int status) {
			this.status = status;
		}

	}

}
